package com.atguigu.tingshu.order.api;

import cn.hutool.core.util.PageUtil;
import com.alibaba.dubbo.container.page.PageHandler;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

    @Autowired
    private OrderInfoService orderInfoService;

    ///api/order/orderInfo/trade
    @GuiGuLogin
	@Operation(summary = "订单确认")
    @PostMapping("orderInfo/trade")
    public Result<OrderInfoVo> trade(@RequestBody TradeVo tradeVo){
        OrderInfoVo orderInfoVo =	orderInfoService.trade(tradeVo);
        return Result.ok(orderInfoVo);
    }

    ///api/order/orderInfo/submitOrder
    @GuiGuLogin
    @Operation(summary = "提交订单")
    @PostMapping("/orderInfo/submitOrder")
    public Result<Map<String,String>> submitOrder(@RequestBody OrderInfoVo orderInfoVo){
        Long userId = AuthContextHolder.getUserId();
        if(userId ==null){
            throw  new GuiguException(400,"用户未登陆");
        }
        Map<String,String> map= orderInfoService.submitOrder(orderInfoVo,userId);
        return Result.ok(map);
    }

    /**
     * 查询当前用户指定订单信息
     *
     * @param orderNo
     * @return
     */
    @GuiGuLogin
    @GetMapping("/orderInfo/getOrderInfo/{orderNo}")
    public Result<OrderInfo> getOrderInfo(@PathVariable("orderNo") String orderNo){

        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderNo);
        return Result.ok(orderInfo);
    }

    @Operation(summary = "分页获取当前用户订单列表")
    @GetMapping("/orderInfo/findUserPage/{page}/{limit}")
    @GuiGuLogin
    public Result<Page<OrderInfo>> findUserPage(@PathVariable Long page,@PathVariable Long limit){
        Long userId = AuthContextHolder.getUserId();
        Page<OrderInfo> orderInfoPage = new Page<>(page,limit);
       orderInfoPage =  orderInfoService.findUserPage(orderInfoPage,userId);
       return Result.ok(orderInfoPage);
    }

    /**
     * 用户支付成功后，修改订单状态
     * @param orderNo 订单编号
     * @return
     */
    @Operation(summary = "用户支付成功后，修改订单状态")
    @GetMapping("/orderInfo/orderPaySuccess/{orderNo}")
    public Result orderPaySuccess(@PathVariable String orderNo){
        orderInfoService.orderPaySuccess(orderNo);
        return Result.ok();
    }



}

